package com.bill99.golden.inf.sso.service;

import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

import com.bill99.golden.inf.sso.session.SessionService;
import com.bill99.golden.inf.sso.util.DaemonThread;
import com.ryan.utils.DateUtil;

public class CheckClientTokenTimeoutTask extends DaemonThread {
	private static Logger logger = Logger.getLogger(CheckClientTokenTimeoutTask.class);
	private static boolean isRun = true;

	@Override
	public void run() {
		super.run();
		while (CheckClientTokenTimeoutTask.isRun()) {
			DateUtil.date2String(new Date());
			Calendar cal = Calendar.getInstance();
			int hour = cal.get(Calendar.HOUR_OF_DAY);
			logger.info("CheckClientTokenTimeoutTask is running......,this time: " + DateUtil.date2String(new Date()));
			if (hour >= 0 && hour <= 5) {
				logger.info("check client token......");
				try {
					ClientTokenService.checkSessionTimeOut();
					SessionService.checkTimeoutSessionObject();
				} catch (Exception e) {
					logger.error(e.getMessage(),e);
				}
				try {
					Thread.sleep(60000 * 40);
				} catch (InterruptedException e) {
					logger.error(e.getMessage(),e);
				}
			} else {
				try {
					Thread.sleep(600000);
				} catch (InterruptedException e) {
					logger.error(e.getMessage(),e);
				}
			}
		}
		logger.info("CheckClientTokenTimeoutTask finished!");
	}

	public static boolean isRun() {
		return isRun;
	}

	public static void setRun(boolean isRun) {
		CheckClientTokenTimeoutTask.isRun = isRun;
	}

}
